package cn.chencaiju.App;

import java.util.Vector;

// 敌方坦克
// 需要做成线程类
class EnemyTank extends Tank implements Runnable {
    int time = 0;
    //定义一个向量存放敌人的子弹
    Vector<Shot> bullet = new Vector<Shot>();
    //敌人添加子弹，在刚刚创建坦克的时候
    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
            switch (Direct) {
            case 0:
                for(int i=0;i<3;i++){
                    if(y-speed>0)
                    y-=speed;
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                //说明坦克正在向上移动
                break;
            case 1:
                for(int i=0;i<3;i++){
                    if(y<MyPanel.backgroundheight-25)
                    y+=speed;
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case 2:
                for(int i=0;i<3;i++){
                    if(x-speed>0)
                    x-=speed;
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case 3:
                for(int i=0;i<3;i++){
                    if(x<MyPanel.backgroundwidth-25)
                    x+=speed;
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
            }
            time++;
            if(time%2==0){
                if(isLive){
                    if(bullet.size()<8){
                        Shot s = null;
                        //没有子弹了  就添加
                        switch(Direct){

                        case 0:
                            // 创建一颗子弹
                            s = new Shot(x + 8, y - 4, 0);
                            // 把子弹加入向量
                            bullet.add(s);
                            break;
                        case 1:
                            s = new Shot(x + 9, y + 32, 1);
                            bullet.add(s);
                            break;
                        case 2:
                            s = new Shot(x - 8, y + 8, 2);
                            bullet.add(s);
                            break;
                        case 3:
                            s = new Shot(x + 32, y + 9, 3);
                            bullet.add(s);
                            break;
                        default:
                            break;
                        }
                        //启动子弹线程
                        Thread t = new Thread(s);
                        t.start();
                    }
                }
            }


            //让坦克随机产生新的方向
            Direct =  (int)(Math.random()*4);
            //判断敌人坦克是否死亡
            if(isLive==false){
                //让坦克死亡，退出线程
                break;
            }

        }
    }
}